import { ApiProperty } from '@nestjs/swagger'
import { Column, Entity, Index, JoinColumn, OneToOne } from 'typeorm'

import { CompleteEntity } from '~/common/entity/common.entity'
import { DictItemEntity } from '~/modules/system/dict-item/dict-item.entity'
import { UserEntity } from '~/modules/user/user.entity'
import { DeviceAttrEntity } from './deviceAttr.entity'

@Entity({ name: 'health_device' })
export class DeviceEntity extends CompleteEntity {
  @Column({ nullable: false, length: 36, comment: '设备ID' })
  @ApiProperty({ description: '设备ID' })
  @Index({ unique: true })
  uid: string

  @Column({ nullable: true, comment: '序列号' })
  @ApiProperty({ description: '序列号' })
  @Index({ unique: true })
  sn: string

  @Column({ nullable: true, length: 50, comment: '设备名称' })
  @ApiProperty({ description: '设备名称' })
  name: string

  @Column({ nullable: true, comment: '设备类型' })
  @ApiProperty({ description: '设备类型' })
  type: number

  @Column({ name: 'user_id', nullable: true, comment: '用户ID' })
  @ApiProperty({ description: '用户ID' })
  userId: number

  @OneToOne(() => UserEntity, user => user.id, {
    onDelete: 'SET NULL',
  })
  @JoinColumn({ name: 'user_id', referencedColumnName: 'id' })
  user: UserEntity

  @OneToOne(() => DeviceAttrEntity, {
    onDelete: 'NO ACTION',
  })
  @JoinColumn({ name: 'uid' })
  attr: DeviceAttrEntity

  @ApiProperty({ description: '类型名称' })
  typeDict: DictItemEntity
}
